package com.hzp.net.netty.server.handler;

import com.hzp.net.netty.server.ServerInfo;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * netty server 通用处理器
 */

@Slf4j
public class NettyServerInHandlerFilter extends ChannelInboundHandlerAdapter {


    List<ChannelHandler> filters = new ArrayList<>();

    public NettyServerInHandlerFilter() {
    }


    @Override
    public void channelRegistered(ChannelHandlerContext ctx){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},注册成功",channel.remoteAddress());
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},注销成功",channel.remoteAddress());
        ctx.fireChannelUnregistered();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},连接就绪",channel.remoteAddress());
        ctx.fireChannelActive();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},断开就绪",channel.remoteAddress());
        ctx.fireChannelInactive();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},读取数据就绪",channel.remoteAddress());
        if (msg instanceof FullHttpRequest) {// 如果是HTTP请求，进行HTTP操作
            FullHttpRequest request = (FullHttpRequest) msg;
            System.out.println(request.content());
            System.out.println("server received: " + request.content());

        } else if (msg instanceof WebSocketFrame) {// 如果是Websocket请求，则进行websocket操作
            System.out.println("WebSocket");
        }
        ctx.fireChannelRead(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},读取数据完成",channel.remoteAddress());
        ctx.fireChannelReadComplete();
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},事件触发",channel.remoteAddress());
        ctx.fireUserEventTriggered(evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{},数据写入修改",channel.remoteAddress());
        ctx.fireChannelWritabilityChanged();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        SocketChannel channel = (SocketChannel) ctx.channel();
        log.info("客户端{}，发生异常:{}",channel.remoteAddress(),cause.getMessage());
        ctx.close();
    }


}
